﻿//消耗计划查询编辑
function viewModel(data) {
    var self = this;
    this.dataSource = data.dataSource;
    this.form = ko.mapping.fromJS(data.form);
    this.grid = {
        size: { w: 4, h: 94 },
        url: '/api/dev/Byjl',
        queryParams: ko.observable(),
        pagination: true,
        customLoad: false
    };
    //刷新
    this.refreshClick = function () {
        window.location.reload();
    };
    //下载
    this.downloadClick = function (vm, event) {
        com.exporter(self.grid).download($(event.currentTarget).attr("suffix"));
    };
    //增加
    this.addClick = function () {
        com.ajax({
            type: 'GET',
            url: "/api/dev/Byjl/getnewbillno",
            success: function (d) {
                com.openTab("计划明细", "/dev/Byjl/edit/" + d);
            }
        });
    }
    //编辑
    this.editClick = function () {
        var row = self.grid.datagrid('getSelected');
        if (!row) return com.message('warning', "请选中某条数据后进行操作！");
        com.openTab("计划明细", "/dev/Byjl/edit/" + row["Byjl_BillNo"], "icon icon-laptop_edit");
    }
    //双击事件
    this.grid.onDblClickRow = this.editClick;
    //删除
    this.deleteClick = function () {
        var row = self.grid.datagrid('getSelected');
        if (!row) return com.message('warning', "请选中某条数据后进行操作！");
        com.message('confirm', "确定要删除选中的单据吗", function (b) {
            if (b) {
                com.ajax({
                    type: 'DELETE',
                    url: "/api/dev/Byjl/" + row["Byjl_BillNo"],
                    success: function () {
                        com.message('success', "删除成功");
                        self.searchClick();
                    }
                });
            }
        });
    }
    //审核
    this.auditClick = function () {
        alert("audit");
    }

    this.searchClick = function () {
        var param = ko.toJS(this.form);
        this.grid.queryParams(param);
    };

    this.clearClick = function () {
        $.each(self.form, function () { this(''); });
        this.searchClick();
    };
}
//明细页面
function viewModelDetail(data) {
    var self = this;
    this.dataSource = data.dataSource;
    this.scrollKeys = ko.mapping.fromJS(data.scrollKeys);       //数据滚动按钮（上一条下一条）
    this.form = ko.mapping.fromJS(data.form || data.defaultForm); //表单数据
    this.setting = data.setting; //主键设置
    this.readonly = ko.computed(function () { return self.form.ApproveState() == "passed"; });

    this.firstClick = function () {
        self.scrollTo(self.scrollKeys.first());
    };
    this.previousClick = function () {
        self.scrollTo(self.scrollKeys.previous());
    };
    this.nextClick = function () {
        self.scrollTo(self.scrollKeys.next());
    };
    this.lastClick = function () {
        self.scrollTo(self.scrollKeys.last());
    };
    this.scrollTo = function (id) {
        if (id == self.scrollKeys.current()) return;
        com.setLocationHashId(id);
        com.ajax({
            type: 'GET',
            url: "/api/dev/Byjl/geteditmaster/" + id,
            success: function (d) {
                ko.mapping.fromJS(d, {}, self);
                ko.mapping.fromJS(d, {}, data);
            }
        });
        //        self.grid.url(self.urls.getdetail + id);
        //        self.grid.datagrid('loaded');
    };
    this.saveClick = function () {

        if (self.readonly()) return;
        var post = {           //传递到后台的数据
            form: com.formChanges(self.form, data.form, self.setting.postFormKeys)
        };
        if ((com.formValidate()) && (post.form._changed)) {
            com.ajax({
                url: "/api/dev/Byjl/edit/",
                data: ko.toJSON(post),
                success: function (d) {
                    com.message('success', "保存成功");
                    ko.mapping.fromJS(post.form, {}, data.form); //更新旧值
                }
            });
        }

    }
    this.rejectClick = function () {
        alert("reject");

    }
    this.auditClick = function () {

        alert("audit");
    }
    this.approveButton = {
        iconCls: ko.computed(function () { return self.form.ApproveState() == "passed" ? "icon-user-reject" : "icon-user-accept"; }),
        text: ko.computed(function () { return self.form.ApproveState() == "passed" ? "反审" : "审核"; })
    };
}